package com.itcast.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.orm.hibernate4.support.HibernateDaoSupport;

import com.itcast.dao.CustomNameDao;
import com.itcast.domain.CustomName;

public class CustomNameDaoImpl extends HibernateDaoSupport implements CustomNameDao{
	
	 private JdbcTemplate jdbcTemplate;  
     
	    public JdbcTemplate getJdbcTemplate() {  
	        return jdbcTemplate;  
	    }  
	     
	    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {  
	        this.jdbcTemplate = jdbcTemplate;  
	    }  

	@Override
	public void save(CustomName customName) {
		this.getHibernateTemplate().save(customName);
	}

	@Override
	public void update(CustomName customName) {
		this.getHibernateTemplate().update(customName);
	}

	@Override
	public void delete(CustomName customName) {
		this.getHibernateTemplate().delete(customName);
	}

	@Override
	public List<String> findById(Integer db_id) {
		String sql = "select * from custom_name where db_id = ? ;";
		RowMapper<CustomName> rowMapper = BeanPropertyRowMapper.newInstance(CustomName.class);
		List<CustomName> result = this.jdbcTemplate.query(sql, rowMapper,db_id);
		List<String> name = new ArrayList<>();
		for(int i = 0;i < result.size();i++){
			name.add(result.get(i).getName());
		}
		return name;
	}

	@Override
	public String findByIdAndNum(Integer db_id, Integer num) {
		String sql = "select * from custom_name where db_id = ? and custom_num = ?;";
		RowMapper<CustomName> rowMapper = BeanPropertyRowMapper.newInstance(CustomName.class);
		List<CustomName> result = this.jdbcTemplate.query(sql, rowMapper,db_id,num);
		if(result.size() > 0){
			return result.get(0).getName();
		}else{
			return null;
		}
	}

}
